********************************************************************************
* Replication file: "Creating crowny capitalists"
* June 24, 2019
* Contact: bernhard.reinsberg@glasgow.ac.uk
********************************************************************************

***********************************************************************  
* Table 1: ICRG index  
***********************************************************************

* Data

  use "Replication_MAIN", clear
  
  replace vdem_corr=100*vdem_corr
  replace vdem_pubcorr=100*vdem_pubcorr
  replace vdem_execorr=100*vdem_execorr 
  
  drop if incid==1
  g lngdppc=ln(gdppc_WDI)
  g lnpop=ln(unna_pop)
  g lnoilpc=ln(1+ross_oil_prod_QOG/unna_pop)
  g oilpc=ross_oil_prod_QOG/unna_pop
  g HRV_index=transparencyindex
  g ssa=(regid==7)
  g lic=(incid==2)

  sort cid year 
  xtset cid year  
  
  g IMFcum=.
  g Loancum=.
  forvalues i=1980/2014{
    g imf=IMFnn
	replace imf=. if year>`i' | year<=`i'-5
	egen IMF=sum(imf), by(cid)
	replace IMFcum=IMF if year==`i'
	drop imf IMF 
	
	g ploan=loan
	replace ploan=. if year>`i' | year<=`i'-5
	egen PLOAN=sum(ploan), by(cid)
	replace Loancum=ln(1+PLOAN) if year==`i'
	drop ploan PLOAN 
  }
  lab var IMFcum "Past programs"
  lab var Loancum "Prior use of IMF credit, logarithmized"
  
  global BMAmax  lngdppc p_durable_QOG p_polity2_QOG  pop_urb_tot_WDI min_gdp_WDI lnoilpc
  global LBMAmax  L.lngdppc L.p_durable_QOG L.p_polity2_QOG  L.pop_urb_tot_WDI L.min_gdp_WDI L.lnoilpc

  global BMAmin  lngdppc pwt_hci_QOG p_durable_QOG p_polity2_QOG pop_urb_tot_WDI gxp_gdp_WDI min_gdp_WDI lnoilpc 
  global LBMAmin  L.lngdppc L.pwt_hci_QOG L.p_durable_QOG L.p_polity2_QOG L.pop_urb_tot_WDI L.gxp_gdp_WDI L.min_gdp_WDI L.lnoilpc

  qui xi:reg icrgPC i.cid i.year i.regid
  set matsize 800
  cmp setup 

  mat LIV=J(6,5,0) 
  mat LIVDiag=J(3,5,0)

* Estimations  
  
forvalues l=1/5{
  
  local k=`l'+1
  di "Level lag=`l'"
  
  global X L`l'.lngdppc L`l'.pwt_hci_QOG L`l'.p_durable_QOG L`l'.p_polity2_QOG L`l'.pop_urb_tot_WDI L`l'.gxp_gdp_WDI L`l'.min_gdp_WDI L`l'.lnoilpc
  global Z L`k'.IMFcum L`k'.s_unga3g7 L`k'.nUnder L`k'.lngdppc L`k'.p_polity2_QOG L`k'.reserves L`k'.gdp_growth_WDI L`k'.debtser_gni L`k'.dpi_exelec _Iregid* _Iyear*
  
  ** Definition of instruments **
  qui egen var2=mean(SCB), by(cid)
  qui gen int2=var2*L`l'.nUnder
  qui egen var3=mean(QCB), by(cid)
  qui gen int3=var3*L`l'.nUnder
  
  qui cmp (icrgPC =L`l'.SCB L`l'.QCB L`l'.IMFnn $X _Icid* _Iyear*) (L`l'.SCB =int2 $X _Icid* _Iyear*) (L`l'.QCB=int3 $X _Icid* _Iyear*) (L`l'.IMFnn=$Z), indicators($cmp_cont $cmp_cont $cmp_cont $cmp_probit) ro cl(cid) iterate(50)
  est store r_sqeff`l'
  mat B=e(b)
  mat S=e(V)
  mat LIV[1,`l']=B[1,1]
  mat LIV[2,`l']=sqrt(S[1,1])
  mat LIV[3,`l']=B[1,2]
  mat LIV[4,`l']=sqrt(S[2,2])
  mat LIV[5,`l']=B[1,3]
  mat LIV[6,`l']=sqrt(S[3,3])
  qui test ([L`l'SCB]int2 [L`l'QCB]int3)
  mat LIVDiag[3,`l']=r(chi2)
  qui xtreg icrgPC int2 int3 L`l'.IMFnn $X _Iyear* if e(sample), fe 
  mat LIVDiag[2,`l']=e(r2_w)	
  mat LIVDiag[1,`l']=e(N)	
  drop var* int*
  }
  
  mat li LIV
  mat li LIVDiag

  ** Table 1 **
  estout r_sqeff*, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(r2_w N F, fmt(%7.2f %7.0f %7.2f)) drop(_cons *year* _I*) 
  
  
